package com.yami.shop.api.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yami.shop.bean.event.AddRightsEvent;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.VrSceneTrigger;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.service.UserService;
import com.yami.shop.service.VrSceneTriggerService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * @author LGH
 */
@RestController
@RequestMapping("/vrSceneTrigger")
@Tag(name = "保存权益记录")
@AllArgsConstructor
public class VrSceneTriggerController {

    private final VrSceneTriggerService vrSceneTriggerService;
    private final UserService userService;
    @Autowired
    private ApplicationContext applicationContext;
    /**
     * vr观看后保存可以发放的权益记录
     * @param vrSceneTrigger
     * @return
     */
    @PostMapping("/saveVrSceneTriggerRecord")
    public ServerResponseEntity saveVrSceneTriggerRecord(@RequestBody VrSceneTrigger vrSceneTrigger) {
        if(StrUtil.isEmpty(vrSceneTrigger.getPhone()) || StrUtil.isEmpty(vrSceneTrigger.getOpenId()) || vrSceneTrigger.getRightsId()==null || ("1".equals(vrSceneTrigger.getWxMsnAuth()) && StrUtil.isEmpty(vrSceneTrigger.getTemplateId()))){
            throw new YamiShopBindException("yami.sys.illegalArgument");
        }
        vrSceneTriggerService.save(vrSceneTrigger);
        return ServerResponseEntity.success();
    }

    /**
     * 通过电话号码 获取可以领取的列表 只领取授权的手机号送的权益
     * @param phone
     * @return
     */
    @GetMapping("/getListByPhone")
    public ServerResponseEntity getListByPhone(@RequestParam String phone) {
        List<VrSceneTrigger> list = vrSceneTriggerService.getListByPhone(phone);
        return ServerResponseEntity.success(list);
    }

    /**
     * 领取
     * @param 发送表主键id,userId
     * @return
     */
    @GetMapping("/receive")
    public ServerResponseEntity receive(@RequestParam String userId,@RequestParam Long id) {
        VrSceneTrigger byId = vrSceneTriggerService.getById(id);
        //修改发送表领取状态
        LambdaUpdateWrapper<VrSceneTrigger> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(VrSceneTrigger::getId,id);
        lambdaUpdateWrapper.set(VrSceneTrigger::getUserId,userId);
        lambdaUpdateWrapper.set(VrSceneTrigger::getStatus,"3");
        lambdaUpdateWrapper.set(VrSceneTrigger::getUpdateTime,new Date());
        vrSceneTriggerService.update(lambdaUpdateWrapper);
        //增加领取权益持仓记录
        applicationContext.publishEvent(new AddRightsEvent(null,userId,null,Long.valueOf(byId.getRightsId())));
        return ServerResponseEntity.success();
    }

    /**
     * 一键领取
     * @param userId
     * @return
     */
    @GetMapping("/receiveBatch")
    public ServerResponseEntity receiveBatch(@RequestParam String userId) {
        //获取该用户下所有授权手机号的发送记录（默认授权手机号和登录手机号一致，前段会备注 只能登录领取授权手机号下的权益）
        User userByUserId = userService.getUserByUserId(userId);
        List<VrSceneTrigger> list = vrSceneTriggerService.getListByPhone(userByUserId.getUserMobile());
        //修改发送表领取状态
        for (VrSceneTrigger vrSceneTrigger: list) {
            vrSceneTrigger.setUpdateTime(new Date());
            vrSceneTrigger.setStatus("3");
            vrSceneTrigger.setUserId(userId);
            //增加领取权益持仓记录
            applicationContext.publishEvent(new AddRightsEvent(null,userId,null,Long.valueOf(vrSceneTrigger.getRightsId())));
        }
        vrSceneTriggerService.updateBatchById(list);
        return ServerResponseEntity.success();
    }
}
